using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
using GameFramework.Audio;
using Microsoft.Xna.Framework.Content;

namespace GameFramework.BasicScreens
{
    public class GameplayScreen : GameScreen
    {
        InputAction _quit_action;
        float pauseAlpha;
        public ContentManager ContentManager;

        public GameplayScreen()
        {
            Audio.AudioManager.PauseSong();
        }

        public override void Activate(bool instancePreserved)
        {
            _quit_action = new InputAction(new Buttons[] { Buttons.Back }, null, true);
            Initialize();
            base.Activate(instancePreserved);
        }

        /// <summary>
        /// Init all your variable
        /// </summary>
        public virtual void Initialize()
        {
            if (ContentManager == null)
            {
                ContentManager = new ContentManager(ScreenManager.Game.Services, "Content");
            }
            LoadContent();
        }

        /// <summary>
        /// Load Textures, Sounds, Font 
        /// </summary>
        protected virtual void LoadContent()
        {

        }

        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            PlayerIndex player;
            if (_quit_action.Evaluate(input, ControllingPlayer, out player))
            {
                OnBackButtonPressed();
            }
            base.HandleInput(gameTime, input);
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);
            if (coveredByOtherScreen)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {
                ActiveUpdate(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            }
            else
            {
                InactiveUpdate(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (TransitionPosition > 0 || pauseAlpha > 0)
            {
                float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);

                ScreenManager.FadeBackBufferToBlack(alpha);
            }
            base.Draw(gameTime);
        }

        public virtual void OnBackButtonPressed()
        {
            this.ExitScreen(); 
            
        }

        public virtual void ActiveUpdate(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

        }

        public void InactiveUpdate(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

        }

        public override void ExitScreen()
        {
            AudioManager.PlaySong("game_music", true);
            ContentManager.Unload();
            base.ExitScreen();
        }
    }
}
